$(function(){
    $("#btn_bj_search").click(function(){
        bj_search();
    }).click();

    //库存报价导入
    $("#btn_bj_inport").click(function(){
        var win = $.window({
            title:"库存报价导入",
            width:450,
            height:240,
            cache:false,
            url:$.webPath() + "/xsgl/kcbj/kcbj_upload",
            modal:true ,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $('#uploadSubject').form('submit', {
                            onSubmit:function(){
                                $(win).mask();
                            } ,
                            success:function (data) {
                                $(win).mask('hide');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(win).window('close');
                                    $('#kcbj_bj').datagrid('reload');
                                    $.messager.show({
                                        title:'提示',
                                        msg:json.message,
                                        timeout:3000,
                                        showType:'slide'
                                    });
                                } else
                                    $.messager.alert('提示', json.message, 'error');
                            }
                        });
                    }
                }
            ]
        });
    });

    //库存报价导出
    $("#btn_bj_export").click(function(){
        window.open($.webPath() + "/xsgl/kcbj/kcbj_export?bjgsId="+$('#bj_bjgs').combobox('getValue')+"&&bjrq="+ $('#bj_bjrq').combobox('getValue'));
    });

    $("#bj_bjgs").combotree({
        url:$.webPath()+'/organ/gsjg/gsjg_tree?type=1',
        disabled:true,
        panelHeight:'200',
        lines:'true',
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});

function bj_search(){
    $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm': 'cd'}, function (cddata) {
        bjGrid(cddata);
    });
}

function bjGrid(cdOptions) {
    $("#kcbj_bj").edatagrid({
        url: $.webPath() + '/xsgl/kcbj/kcList',
        fitColumns: true,
        singleSelect: true,
        fit: true,
        checkbox: true,
        checkOnSelect: false,
        selectOnCheck: false,
        rownumbers: true,
        queryParams:{
            bjgsId:$("#kcbj_bj_toolbar input[name='bjgsId']").val(),
            bjrq:$("#bj_bjrq").datebox("getValue") || ''
        },
        toolbar: "#kcbj_bj_toolbar",
        columns: [
            [
                {field: 'id', title: 'ID', hidden:true},
                {field: 'gg.name', title: '物资名称', width: 50},
                {field: 'gg.cz', title: '材质', width: 30},
                {field: 'gg.ggxh', title: '规格型号', width: 30},
                {field: 'gg.dw', title: '单位', width: 20},
                {field: 'cd.z', title: '产地', width: 20},
                {field: 'kcsl', title: '库存数量', width: 30,
                    formatter:function(value){
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field: 'kkdsl', title: '可开单数量', width: 30,
                    formatter:function(value){
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field: 'bj', title: '报价', width: 30,editor: {type: 'numberbox', options: {precision: 4, min: 0,groupSeparator:','}},  formatter:function(value){
                    var val = formatJeForHtml(value);
                    return val;
                }},
                {field: 'scbj', title: '市场报价', width: 30,editor: {type: 'numberbox', options: {precision: 4, min: 0,groupSeparator:','}},  formatter:function(value){
                    var val = formatJeForHtml(value);
                    return val;
                }},
                {field: 'wj', title: '网价', width: 30,editor: {type: 'numberbox', options: {precision: 4, min: 0,groupSeparator:','}},
                    formatter:function(value){
                     var val = formatJeForHtml(value);
                    return val;
                }}
            ]
        ],
        onSelect: function (rowIndex) {
            //获取每个单元格
            $("#kcbj_bj").edatagrid('editRow', rowIndex);
        }
    });
}